<?php
require_once '../sub/init.php';
define('ZEAI2',dirname(__FILE__).DIRECTORY_SEPARATOR);
require_once 'chkUadm.php';
if($session_kind == 'crm'){
	if(!in_array('crm_u_add',$QXARR))exit(noauth('暂无【用户录入】权限'));
}else{
	if(!in_array('u_add',$QXARR))exit(noauth('暂无【用户录入】权限'));
}
require_once ZEAI.'cache/udata.php';$sex_ARR = json_decode($_UDATA['sex'],true);
header("Cache-control: private");
if ($submitok == "ajax_user_chkmob") {
	$mob=trimhtml($mob);
	if (!ifmob($mob))json_exit(array('flag'=>0,'msg'=>'请输入正确的手机'));
	$row = $db->ROW(__TBL_USER__,"id","mob='".$mob."' OR mob2='".$mob."'","name");
	if ($row){
		json_exit(array('flag'=>0,'msg'=>'【'.$mob.'】手机已被【UID：'.$row['id'].'】占用，请重新输入'));
	}else{
		json_exit(array('flag'=>1,'msg'=>'OK'));
	}
}elseif($submitok == "addupdate") {
	if (str_len($uname) > 20 || str_len($uname)<3)alert_adm("请输入正确的用户名",-1);
	if (str_len($pwd) > 20 || str_len($pwd)<6)alert_adm("请输入正确的密码",-1);
	if (!ifint($kind))alert_adm("请输入选择用户类型",-1);
	$mob=trimhtml($mob);
	$mob2=trimhtml($mob2);
	$row = $db->ROW(__TBL_USER__,"id","mob='".$mob."' OR mob2='".$mob."'","name");
	if ($row)alert_adm('手机已被【UID：'.$row['id'].'】占用，请重新输入',-1);
	//$row = $db->NUM('yzlove.com___zeai.cn','id',"uname='".$uname."'");if($row)alert_adm("此用户名已被占用,请重新输入",-1);
	
	$sex = (empty($sex))?1:intval($sex);
	$uname    = dataIO($uname,'in');
	$nickname = trimhtml(dataIO($nickname,'in'));
	$truename = trimhtml(dataIO($truename,'in',12));
	$identitynum = trimhtml(dataIO($identitynum,'in',18));
	$crm_ubz = trimhtml(dataIO($crm_ubz,'in',500));
	if(!empty($identitynum)){
		$row = $db->ROW(__TBL_USER__,"id","identitynum='".$identitynum."' AND identitynum<>''","name");
		if ($row)alert_adm('【'.$identitynum.'】身份证号已被【UID：'.$row['id'].'】占用，请重新输入',-1);
	}
	$pwd      = md5(trim($pwd));
	$kind = intval($kind);
	$birthday = (!ifdate($birthday))?'0000-00-00':$birthday;
	$flag    = ($flag==1)?1:-2;
	$dataflag=1;
	
	$SETSQL = "";
	if(ifint($agentid)){
		$row3 = $db->ROW(__TBL_CRM_AGENT__,"title","id=".$agentid,"num");
		if ($row3)$SETSQL .= ",agentid=$agentid,agenttitle='".$row3[0]."'";
	}else{$SETSQL .= ",agentid=0,agenttitle=''";}
	if($admid>0){
		$row3 = $db->ROW(__TBL_CRM_HN__,"truename","id=".$admid,"num");
		if ($row3)$SETSQL .= ",admid=$admid,admtime=".ADDTIME.",admname='".$row3[0]."'";
	}else{$SETSQL .= ",admid=0,admname=''";}
	if($ifmobrz==1)$SETSQL .= ",RZ='mob'";
	$crm_ukind = intval($crm_ukind);
	$crm_fromclue = intval($crm_fromclue);
	$db->query("INSERT INTO ".__TBL_USER__." (kind,uname,nickname,pwd,regtime,endtime,refresh_time,regkind,sex,birthday,flag,crm_ukind,crm_fromclue,mob,mob2,truename,identitynum,crm_ubz) VALUES ($kind,'".$uname."','".$nickname."','".$pwd."',".ADDTIME.",".ADDTIME.",".ADDTIME.",9,$sex,'".$birthday."',$flag,$crm_ukind,$crm_fromclue,'$mob','$mob2','$truename','$identitynum','$crm_ubz')");
	$uid = intval($db->insert_id());
	
	$db->query("UPDATE ".__TBL_USER__." SET dataflag='$dataflag'".$SETSQL." WHERE id=".$uid);
	shalou_init($uid);
	//
	$row_shalou=shalou_data($uid);
	$row_shalou['truename']=$truename;
	$row_shalou['identitynum']=$identitynum;
	$new_row_shalou=encode_json($row_shalou);
	$db->query("UPDATE ".__TBL_SHALOU__." SET jsonstr='$new_row_shalou' WHERE uid=".$uid);
	//
	sea_mod_time($uid);
	set_data_ed_bfb($uid);
	AddLog('录入新用户【'.$nickname.'（uid:'.$uid.'）】');
	//alert_adm("录入成功，请继续完善资料".$cc,"");
	header("Location: u_mod_data.php?submitok=mod&t=2&uid=".$uid);
}
$birthday = date("Y")-20-cdstr(1);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/cache/udata.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="js/select2.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/res/select4.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script>
function chkform(){
	if(!zeai.ifmob(mob.value)){
		zeai.msg('请输入正确的手机号码',mob);
		return false;
	}
	zeai.ajax('u_add'+zeai.ajxext+'submitok=ajax_user_chkmob&mob='+mob.value,function(e){var rs = zeai.jsoneval(e);
		if (rs.flag == 1){
			if(!zeai.empty(mob2.value)){
				zeai.ajax('u_add'+zeai.ajxext+'submitok=ajax_user_chkmob&mob='+mob2.value,function(e){var rs = zeai.jsoneval(e);
					if (rs.flag == 1){
						chkF();
					}else{
						zeai.msg(rs.msg,mob2);
					}
				});
			}else{
				chkF();
			}
		}else{
			zeai.msg(rs.msg,mob);
		}
	});
	function chkF(){
		if(!zeai.form.ifradio('sex')){
			zeai.msg('请选择用户性别');
			return false;
		}
		if(zeai.empty(birthday.value)){
			zeai.msg('请选择出生年月',birthday);
			return false;
		}
		if(!zeai.form.ifradio('kind')){
			zeai.msg('请选择用户类型');
			return false;
		}
		if(zeai.empty(nickname.value) || zeai.str_len(nickname.value)<2 || zeai.str_len(nickname.value)>40){
			zeai.msg('请输入正确的昵称',nickname);
			return false;
		}
		GYLform.submit();
	}	
}
function user_chkmob(mob){
	if(!zeai.ifmob(mob)){
		zeai.msg('请输入正确的手机号码');
		return false;
	}
	zeai.ajax('u_add'+zeai.ajxext+'submitok=ajax_user_chkmob&mob='+mob,function(e){var rs = zeai.jsoneval(e);
		if (rs.flag != 1)zeai.msg(rs.msg);
	});
}
<?php
$rt=$db->query("SELECT id,title FROM ".__TBL_CRM_AGENT__." WHERE flag=1 ORDER BY px DESC,id DESC");
$total = $db->num_rows($rt);
if ($total > 0) {
	$arr=array();$arr2=array();
	for($i=0;$i<$total;$i++) {
		$rows = $db->fetch_array($rt,'name');
		if(!$rows) break;
		$arr2 = array();
		$rt2=$db->query("SELECT id,truename,username FROM ".__TBL_CRM_HN__." WHERE agentid=".$rows['id']." AND kind='crm' AND flag=1 ORDER BY px DESC,id DESC");
		$total2 = $db->num_rows($rt2);
		if ($total2 > 0) {
			for($i2=1;$i2<=$total2;$i2++) {
				$rows2 = $db->fetch_array($rt2,'name');
				if(!$rows2)break;
				$arr2[] = array('id'=>$rows2['id'],'name'=>$rows2['truename'].'　|　'.$rows2['username'].'　|　'.$rows2['id']);
			}
		}
		$arr[] = array('id'=>$rows['id'],'name'=>$rows['title'],'child'=>$arr2);
	}
	$agent_adm_data = encode_json($arr);
}
?>
var agent_adm_data = <?php echo $agent_adm_data;?>
</script>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<style>
input:-webkit-autofill{-webkit-box-shadow:0 0 0px 1000px white inset !important}
.box{min-width:1200px;padding:0}
.table {width:1000px;border:#F0F3F6 1px solid}
.table td{padding:8px;border:0;border-bottom:1px solid #F0F3F6}
.table .tdL{width:100px;background-color:#fafafa}
.table .tbodyT{font-size:18px;height:60px}
.table .tbodyT h6{color:#999;display:block}
</style>
</head>
<body>
<form name="GYLform" id="GYLform" action="<?php echo SELF; ?>" method="post">
<div class="box">
	<div class="boxL">
        <a href="u_add.php" class="ed">用户人工录入</a>
        <a href="import.php">Excel表导入</a>
        <a href="import_weiyuan.php">微缘数据导入</a>
        <a href="import_cfy.php">楚房云数据导入</a>
    </div>
	<div class="boxR">
        <table class="table" style="margin:15px 0 15px 20px">
        <tr><td colspan="4" class="tbodyT"><center>用户人工录入<h6><font class="Cf00">*</font>号为必填，【手机/姓名/身份证】一经录入不能修改（管理员可改）</h6></center></td></tr>

        <tr><td class="tdL">用户归属</td><td colspan="3" class="tdR">
        <select name="agentid" id="agentid" class="size2 W200"></select>　
        <select name="admid" id="admid" class="size2 W300"></select>
		<script>select2({id1:agentid,id2:admid,ext1:'class="size2 W200"',ext2:'class="size2 W200"',nulltext1:'请选择门店',nulltext2:'选择认领红娘',defv1:'<?php echo $session_agentid;?>',defv2:'<?php echo $session_uid;?>',data:agent_adm_data});</script>
        <div class="tips2">选择归属后，前端个人主页点【红娘牵线】将出现对应红娘的微信二维码，不选即为公海资源</div>
        </td></tr>

        <tr><td class="tdL">线索来源</td><td class="tdR"><script>zeai_cn__CreateFormItem('select','crm_fromclue','<?php echo $crm_fromclue; ?>','class="size2 W200"',crm_fromclue_ARR);</script></td>
          <td class="tdL">客户分类</td>
          <td class="tdR"><script>zeai_cn__CreateFormItem('select','crm_ukind','<?php echo $crm_ukind; ?>','class="size2 W200"',crm_ukind_ARR);</script></td>
        </tr>

        <tr>
        <td class="tdL"><font class="Cf00">*</font> 手机号码</td>
        <td class="tdR C8d">
        <input name="mob" id="mob" type="text" class="input size2 W200" size="40" maxlength="11" autocomplete="off" onBlur="user_chkmob(this.value)" />　
        <input type="checkbox" name="ifmobrz" id="ifmobrz" class="checkskin" value="1" checked><label for="ifmobrz" class="checkskin-label"><i class="i1"></i><b class="S12 W100 S14">点亮认证图标</b></label>
        </td>
          <td class="tdL">备用手机</td>
          <td class="tdR"><input name="mob2" id="mob2" type="text" class="input size2 W200" size="40" maxlength="11" autocomplete="off" onBlur="user_chkmob(this.value)" /></td>
        </tr>
        
        <tr><td class="tdL"><font class="Cf00">*</font> 性　　别</td><td class="tdR">
        <?php foreach ($sex_ARR as $v) {?>
            <input type="radio" name="sex" value="<?php echo $v['i'];?>" id="sex_<?php echo $v['i'];?>" class="radioskin"><label for="sex_<?php echo $v['i'];?>" class="radioskin-label"><i class="i1"></i><b class="W30"><?php echo $v['v'];?></b></label>　
        <?php }?>
        </td>
          <td class="tdL"><font class="Cf00">*</font> 生　　日</td>
          <td class="tdR"><input name="birthday" id="birthday" type="text" readonly class="input size2 W120 hand" value="<?php echo $birthday; ?>-01-15" size="10" maxlength="10"></td>
        </tr>
        
        <tr><td class="tdL">真实姓名</td><td class="tdR"><input name="truename" id="truename" type="text" class="input size2 W200" size="40" maxlength="15" autocomplete="off" /><span class="tips">前台不显示</span></td>
          <td class="tdL">身份证号</td>
          <td class="tdR"><input name="identitynum" type="text" id="identitynum" value="" class="input size2 W200" maxlength="18"><span class="tips">前台不显示</span></td>
        </tr>
        
        
        <tr><td class="tdL"><font class="Cf00">*</font> 用户类型</td><td colspan="3" class="tdR" style="padding:20px 10px">
        <input type="radio" name="kind" value="1" id="kind_1" class="radioskin"<?php echo ($kind == 1)?' checked':'';?>><label for="kind_1" class="radioskin-label"><i class="i1"></i><b class="W120">线上用户</b></label><span class="tips">用户可以登录前台网站，可以自主联系互动，和用户自己注册效果一样</span><br><br>
        <input type="radio" name="kind" value="2" id="kind_2" class="radioskin"<?php echo ($kind == 2)?' checked':'';?>><label for="kind_2" class="radioskin-label"><i class="i1"></i><b class="W120">线下用户</b></label><span class="tips">内部用户，网站前台只展示，别的用户联系必须通过红娘，不可登录互动，后台/CRM人工管理服务</span><br><br>
        <input type="radio" name="kind" value="3" id="kind_3" class="radioskin"<?php echo ($kind == 3)?' checked':'';?>><label for="kind_3" class="radioskin-label"><i class="i1"></i><b class="W120">均可(线上+线下)</b></label><span class="tips">都可以，如果线上用户申请红娘委托之后，将自动变为这种类型，后台/CRM人工管理服务</span><br><br>
        <input type="radio" name="kind" value="4" id="kind_4" class="radioskin"<?php echo ($kind == 4)?' checked':'';?>><label for="kind_4" class="radioskin-label"><i class="i1"></i><b class="W120">机器人</b></label>
        <span class="tips">虚拟用户，主要用来自动发私信和打招呼给新注册用户的，也就是虚拟用户（一般不要选）</span>
        </td></tr>
        
        <tr><td class="tdL"><font class="Cf00">*</font> 昵称网名</td><td colspan="3" class="tdR"><input name="nickname" id="nickname" type="text" class="input size2 W200" size="40" maxlength="20" autocomplete="off" /><span class="tips">20字节以内，前台显示</span></td></tr>
        
        <tr><td class="tdL">前台展示</td><td colspan="3" class="tdR">
        <input type="checkbox" name="flag" id="flag" class="switch" value="1" ><label for="flag" class="switch-label"><i></i><b>开启</b><b>关闭</b></label>
        <span class="tips">开启后，将在线上平台展示；关闭后，个人资料隐藏，不对外公开展示</span>
        </td></tr>

        <tr><td class="tdL">CRM备注</td><td colspan="3" class="tdR">
        <input name="crm_ubz" type="text" class="input size2 W100_" maxlength="200" placeholder="其它备注信息"></textarea>
        </td></tr>

 
        <tr>
          <td colspan="4" align="center">
            <input name="submitok" type="hidden" value="addupdate" />
            <button type="button" class="btn size3" onclick="chkform();">　下一步　</button>
            <div style="padding:10px;color:#999">点击下一步进行详细个人资料录入</div>
            <input name="uname" id="uname" type="hidden" class="input size2 W200" value="ht_<?php echo cdnumletters(6); ?>" size="20" maxlength="20"   autocomplete="off" />
            <input name="pwd" value="<?php echo cdnumletters(6);?>" id="pwd" type="hidden" class="input size2 W200"  size="20" maxlength="20"   autocomplete="off" />
          </td>
          </tr>
        </table>
    </div>
    <div class="clear"></div>
</div>

</form>

<script src="laydate/laydate.js"></script><script>lay('#version').html('-v'+ laydate.v);laydate.render({elem: '#birthday'});</script>
<?php require_once 'bottomadm.php';?>